package com.etime.shuhuiyuan.services.lesson.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.shuhuiyuan.dao.LessonDao;
import com.etime.shuhuiyuan.pojo.Lesson;
import com.etime.shuhuiyuan.services.lesson.interfaces.LessonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

@Service
public class LessonServiceImpl implements LessonService {
    //注入
    @Autowired
    private LessonDao lessonDao;

    @Override
    public int insertTxtLesson(Lesson lesson) {
        //获取当前时间为创建时间插入数据库
        long timeSteamp = System.currentTimeMillis();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        String newTime = simpleDateFormat.format(timeSteamp);
        lesson.setLessoncreatetime(newTime);
        return lessonDao.insert(lesson);
    }

    @Override
    public int updateTxtLesson(Lesson lesson) {
        //获取当前修改时间
        long time = System.currentTimeMillis();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.getDefault());
        String formatTime = simpleDateFormat.format(time);
        lesson.setLessonupdatetime(formatTime);
        return lessonDao.updateById(lesson);
    }

    @Override
    public Page<Lesson> selectLessonByPage(Lesson lesson, Long pagesize, Long pagenum) {
        Page<Lesson> page = new Page<>(pagesize,pagenum);
        QueryWrapper<Lesson> wrapper = new QueryWrapper<>();
        //非空判断
        if (lesson.getLessonid()!=null && lesson.getLessonid()!=-1){
            wrapper.eq("lessonid",lesson.getLessonid());
        }
        return lessonDao.selectPage(page,wrapper);
    }

    @Override
    public List<Lesson> selectLessonList(Lesson lesson) {
        QueryWrapper<Lesson> wrapper = new QueryWrapper<>();
        //非空判断
        if (lesson.getLessonid()!=null && lesson.getChapterid()!=null &&lesson.getLessonid()!=-1){
            wrapper.eq("lessonid",lesson.getLessonid());
        }
        return lessonDao.selectList(wrapper);
    }

    @Override
    public Lesson selectLessonById(Integer id) {
        return lessonDao.selectById(id);
    }

    @Override
    public List<Lesson> selectLessonByChaperid(Integer chapterid) {
        return lessonDao.selectLessonByChapterid(chapterid);
    }


}
